<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use app\modules\Supplier\models\Supplier;
use kartik\select2\Select2;
use kartik\file\FileInput;

//获取所有供应商 中国获取中国 美国获取美国
$data_s = Supplier::find()->where(['nation'=>\yii::$app->user->identity->nation])->all();

//获取所有分组
$data_supplier_type = \yii::$app->params['supplier_type'];
$data_supplier = [];
if($data_s){
   foreach($data_s as $key=>$supplier){
       $data_supplier[$data_supplier_type[$supplier->type]][$supplier->id] = $supplier->en_name; 
   }
}

?>

<div class="cost-form">

      <?php $form = ActiveForm::begin([
                 'id' => 'dynamic-form',
                 'options' => ['enctype' => 'multipart/form-data',],
                 'enableAjaxValidation' => true, // 开启ajax验证
                 'enableClientValidation' => true,
                 'validationUrl' => Url::toRoute(['validate-form']),
                 'layout' => 'horizontal',
                 'fieldConfig' => [
                    'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                    'horizontalCssClasses' => [
                        'label' => 'col-sm-3',
                        'wrapper' => 'col-sm-8',
                        'error' => '',
                    ],
                 ],
     ]); ?>


    <?= Html::activeHiddenInput($model, "product_id");?>

    <?= $form->field($model, 'cost_type')->inline(true)->radioList(Yii::$app->params['cost_type']) ?>

    <?= $form->field($model, 'payment')->inline(true)->radioList(Yii::$app->params['payment_type']) ?>

    <?= $form->field($model, 'supplier_id')->widget(Select2::classname(), [  
                              'language' => 'zh-cn',
                              'options' => ['multiple'=>false,'placeholder' => '选择供应商'],
                              'data' => $data_supplier,
                              'showToggleAll' => false,
                              'pluginOptions' => [
                                  'allowClear' => true,
                              ],
                          ])?>
    <div id="day_num" style="<?=$model->supplier_type == 1 ? 'display:block' :'display: none';?>">
      <?= $form->field($model, 'day_num')->textInput() ?>
    </div>

    <?= $form->field($model, 'price')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'remark')->textArea(['maxlength' => true]) ?>

    <?= $form->field($model, 'file_path')->hint("")->widget(FileInput::classname(), ['language' => 'zh','options' => ['multiple'=>false, 'value'=>$model->file_path],
                                                    'pluginOptions' => [
                                                           'initialCaption'=>$model->file_path,
                                                           'showPreview' => false,
                                                           'showCaption' => true,
                                                           'showRemove' => false,
                                                           'showUpload' => false,
                                                           'browseLabel' =>  '附件',
                                                           'removeLabel' =>  '删除'
                                                       ]  
                                                ])->label("附件") ?>
    <div class="form-group text-center">
        <?= Html::submitButton(Yii::t('app', 'Save'), ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">  

    $("#cost-supplier_id").change(function(){

         var id = $(this).val();
         $.get("<?=Url::toRoute(['/cost/cost/supplier-type'])?>", {id:id}, function(data){

            if(data == 1){
              $("#day_num").show();
            }else{
              $("#day_num").hide();
            }

        })
    });
    
</script>
<?php app\components\JsBlock::end() ?>